<?php
/**
 * Created by PhpStorm.
 * User: lzhy@lzhy.me
 * Date: 2019/4/12 0012
 * Time: 14:45
 */
namespace app\admin\controller;

use app\admin\event\Dt;
use app\admin\event\Utils;
use think\Db;
use think\facade\Request;

class Menu extends Base
{
    public function index()
    {
        if(Request::isPost()){
            $dt = new Dt();
            $data = $dt->table('menu')->searchField('title,path')->fetch();
            foreach ($data['data'] as &$v){
                $editAction   = url('edit',['id'=>$v['id']]);
                $removeAction = url('del',['id'=>$v['id']]);
                $v['buttons'] = sprintf('<a href="#" class="btn-edit" data-action="%s"><i class="fa fa-edit"></i></a>&nbsp;&nbsp;<a href="#" class="btn-remove" data-action="%s"><i class="fa fa-remove"></i></a>',$editAction,$removeAction);
            }
            exit(json_encode($data));
        }

        $tColumns = [
            ['data'=>'id','title'=>'自动编号','orderable'=>true],
            ['data'=>'title','title'=>'标题','orderable'=>false],
            ['data'=>'path','title'=>'url','orderable'=>false],
            ['data'=>'sort','title'=>'排序'],
            ['data'=>'add_time','title'=>'添加时间'],
            ['data'=>'buttons','title'=>'操作','orderable'=>false],
        ];
        $this->assign(['columns'=>json_encode($tColumns)]);
        return view();
    }

    public function edit()
    {
        if(Request::isPost()){
            $params = Request::param();
            $res = ['success'=>0,'msg'=>'请填写完整后再提交'];
            if($params['title']){
                $id = $params['id'];
                try{
                    $data = [
                        'title' => $params['title'],
                        'path'  => trim($params['path']),
                        'pid'   => $params['pid'],
                        'role'  => join(',',$params['role']),
                        'icon'  => trim($params['icon']),
                        'sort'  => $params['sort'] ?? 0,
                        'update_time' => date('Y-m-d H:i:s')
                    ];
                    if($id){
                        $rel = Db::table('menu')->where('id',$id)->update($data);
                        $res = ['success'=>0,'msg'=>'更新失败，请重试'];
                        if($rel){
                            $res = ['success'=>1,'msg'=>'更新成功'];
                        }
                    }else{
                        $data['add_time'] = date('Y-m-d H:i:s');
                        $rel = Db::table('menu')->insert($data);
                        $res = ['success'=>0,'msg'=>'添加失败，请重试'];
                        if($rel){
                            $res = ['success'=>1,'msg'=>'添加成功'];
                        }
                    }
                }catch (\Exception $e){
                    $res = ['success'=>0,'msg'=>'操作失败，请稍后再试','err'=>$e->getMessage()];
                }
            }

            Utils::ifrResponse($res);
        }

        $id = Request::param('id');
        $data = [];
        if($id){
            $data = Db::table('menu')->where('id',$id)->find();
            $data['role'] = explode(',',$data['role']);
        }
        $topMenuList = Db::table('menu')
            ->field('id,title')
            ->where('is_delete',0)
            ->where('pid',0)
            ->where('id','neq',$id)
            ->cache(5)
            ->select();
        array_unshift($topMenuList,['id'=>0,'title'=>'ROOT']);

        $roleList = Db::table('role')
            ->field('id,name,mark')
            ->where('is_delete',0)
            ->cache(5)
            ->select();

        $field = [
            ['label'=>'','name'=>'id','type'=>'hidden'],
            ['label'=>'父级菜单','name'=>'pid','type'=>'select2','options'=>array_column($topMenuList,'title','id')],
            ['label'=>'名称','name'=>'title'],
            ['label'=>'图标','name'=>'icon','type'=>'icon','tips'=>''],
            ['label'=>'路径','name'=>'path'],
            ['label'=>'角色','name'=>'role','type'=>'select2','options'=>array_column($roleList,'name','id'),'multiple'=>true],
            ['label'=>'排序','name'=>'sort','type'=>'number','tips'=>'菜单排序,数字越大越靠后']
        ];
        return parent::_buildModalEditForm('菜单编辑',$field,$data);
    }

    public function del()
    {
        $id = Request::param('id');
        Utils::rowDel('menu',$id);
    }
}